<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Ruby MySQL Tutorial: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Ruby Study Notes - Best Ruby Guide, Ruby Tutorial" />
<meta name="keywords" content="ruby mysql tutorial,ruby study notes,ruby guide,free ruby programming course,best ruby guide,ruby tutorials,ruby tutorial,learn ruby,ruby,ruby on rails,ruby rails,ruby learning,ruby tutoring,learning ruby,ruby programming,ruby on rails development,ruby training" />
<meta name="Distribution" content="Global" />
<meta name="author" content="Satish Talim / Original design: Erwin Aligam - ealigam@gmail.com" />
<meta name="copyright" content="Satish Talim 2007 and beyond..." />
<meta name="verify-v1" content="rFu86se+IkbtF+bH8mgJBKwU5HnKaSd8Ghw9umXQOkM=" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Expires" content="0" />
<meta name="revisit-after" content="1 days" />
<link rel="stylesheet" href="/images/NewOrange.css" type="text/css" />
<link rel="stylesheet" href="/images/syntaxhighlighter.css" type="text/css" />
<link rel="icon" type="image/ico" href="/images/favicon.ico" />
<!-- Google Analytics code -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-59044-10']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- Google Analytics code ends -->
</head>

<body>
<!-- wrap starts here -->
<div id="wrap">

    <div id="header">

        <h1 id="logo">Ruby<span class="orange">Learning.github.io</span></h1>
        <h2 id="slogan">Ruby helps programmers have more fun!</h2>

    </div>

    <div id="menu">
        <ul>
            <li><a href="/" title="Home page for rubylearning.github.io">Home</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Get started Learning Ruby here...">Tutorial</a></li>
            <li><a href="/download/downloads.html" title="Download this tutorial as an eBook">Downloads</a></li>
            <li><a href="/other/testimonials.html" title="People around the world who benefited from this site">Testimonials</a></li>
            <li><a href="/other/ruby_gurus.html" title="Interviews I took of Ruby Gurus around the world">Gurus</a></li>
            <li><a href="/other/certification.html" title="Get certified in Ruby">Certification</a></li>
            <li><a href="/satishtalim/ruby_guide.html" title="Ruby Guide, Mentor">Mentor</a></li>
            <li><a href="/other/ruby_video_search.html" title="Watch videos on Ruby/Rails">Videos</a></li>
            <li><a href="/blog/" title="Ruby blog of Learning Ruby site">Blog</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Online Ruby Course">Online Course</a></li>
            <li><a href="/satishtalim/about.html" title="Information about Satish Talim">About</a></li>
        </ul>
    </div>

    <!-- content-wrap starts here -->
    <div id="content-wrap">

            <div id="main">

                <div id="main-inner"><a name="TemplateInfo"></a>
                <h1>Ruby MySQL Tutorial</h1>

                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/ruby_internet_services.html">&lt;Ruby Internet Services | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/ruby_activerecord_and_mysql.html">ActiveRecord and MySQL&gt;</a>
                  </strong>
                </p>

                <p>This brief <strong>Ruby MySQL Tutorial</strong> shows you how you can connect to MySQL in Ruby. MySQL support in Ruby was made possible by Tomita Masahiro. He has developed a pure Ruby binding called Ruby/MySQL.<br /><br />We need to install the same on our PC and the installation (you need to be connected to the internet and it takes some time) is as shown below:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                C:\>gem install mysql
                Bulk updating Gem source index for: http://gems.rubyforge.org
                Select which gem to install for your platform (i386-mswin32)
                   1. mysql 2.7.3 (mswin32)
                   2. mysql 2.7.1 (mswin32)
                   3. mysql 2.7 (ruby)
                   4. mysql 2.6 (ruby)
                   5. Skip this gem
                   6. Cancel installation
                > 1
                Successfully installed mysql-2.7.3-mswin32
                Installing ri documentation for mysql-2.7.3-mswin32...
                Installing RDoc documentation for mysql-2.7.3-mswin32...
                C:\>
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>This installs mysql-2.7.3-mswin32 driver which is faster and supports MySQL 5.0.51a  and later. Also, it requires Ruby 1.8.6. The documentation for this driver is <a href="http://tmtm.org/en/mysql/ruby/" >here</a>.</p>

                <p>I will assume that you've already installed MySQL 5 or above on your PC and that you have it running and are familiar with the basics.</p>

                <p>First, ensure that your MySQL server is running. If not, you can start it by typing this at the command prompt:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                C:\>mysqld
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>Now, open a new command window run the mysql client program from the command line, as:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                C:\>mysql --user=root mysql
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>You should get the mysql prompt. Next, create a database ruby as:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                mysql> create database ruby;
                Query OK, 1 row affected (0.02 sec)
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>Next, create a table student in the database ruby as:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                mysql> use ruby;
                create table student (id VARCHAR(2), name VARCHAR(20), rank VARCHAR(2));
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>Populate the above table with some data and as a first exercise we try to connect to the MySQL server and print all the names in the table student. Program <strong>p078rubymysql.rb</strong></p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                require 'mysql'

                #my = Mysql.new(hostname, username, password, databasename)
                con = Mysql.new('localhost', '', '', 'ruby')
                rs = con.query('select * from student')
                rs.each_hash { |h| puts h['name']}
                con.close
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>It is as simple as that. You can explore this api further.</p>

                <p style="background-color: #FAFAFA; padding: 5px; margin-top: 20px; font-size: 65%;"><strong>Note</strong>: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various <span style="font-weight: bold;" title="Click Gurus on the menu above">Ruby Gurus</span>), my acknowledgment and thanks to all of them.</p>

                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/ruby_internet_services.html">&lt;Ruby Internet Services | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/ruby_activerecord_and_mysql.html">ActiveRecord and MySQL&gt;</a>
                  </strong>
                </p>

            </div>
            <!-- main inner ends here -->
        </div>

            <div id="rightbar">
            </div>

    <!-- content-wrap ends here -->
    </div>

<!-- wrap ends here -->
</div>

<!-- footer starts here -->
<div id="footer">
    <!-- CHANGE THE FOOTER -->
    <p>&copy; 2006-2021 <strong>rubylearning.github.io - A Ruby Tutorial</strong>&nbsp;&nbsp;Page Updated: 5th Jan. 2021 | Design: <a href="mailto:ealigam@gmail.com">Erwin Aligam</a> | Valid: <a href="http://validator.w3.org/check/referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/">Home</a> | <a href="/privacy.html">Privacy</a> | <a href="/sitemap.html">Sitemap</a></p>
</div>

<!-- footer ends here -->

<!-- SyntaxHighlighter code -->
<script src="/js/shCore.js" type="text/javascript"></script>
<script src="/js/shBrushRuby.js" type="text/javascript"></script>
<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!-- SyntaxHighlighter code -->

</body>
</html>
